﻿//using System;
//using System.Collections.Generic;
//using System.Drawing;
//using System.Drawing.Drawing2D;
//using System.Drawing.Imaging;
//using System.IO;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;

//namespace xycn.common
//{
//    /// <summary>
//    ///
//    /// </summary>
//    public class ImageHelper
//    {
//        /// <summary>
//        /// 将Base64的文字转换为图片
//        /// </summary>
//        /// <param name="base64string">The base64string.</param>
//        /// <returns></returns>
//        public static Bitmap GetImageFromBase64(string base64string)
//        {
//            byte[] buffer = Convert.FromBase64String(base64string);
//            MemoryStream stream = new MemoryStream(buffer);
//            return new Bitmap(stream);
//        }

//        /// <summary>
//        /// 将图片转换为Base64字符串
//        /// </summary>
//        /// <param name="imagefile">The imagefile.</param>
//        /// <returns></returns>
//        /// <exception cref="T:System.Exception">Something wrong during convert!</exception>
//        public static string GetBase64FromImage(string imagefile)
//        {
//            string text = "";
//            try
//            {
//                Bitmap bitmap = new Bitmap(imagefile);
//                MemoryStream memoryStream = new MemoryStream();
//                bitmap.Save(memoryStream, ImageFormat.Jpeg);
//                byte[] array = new byte[memoryStream.Length];
//                memoryStream.Position = 0L;
//                memoryStream.Read(array, 0, (int)memoryStream.Length);
//                memoryStream.Close();
//                return Convert.ToBase64String(array);
//            }
//            catch (Exception)
//            {
//                throw new Exception("Something wrong during convert!");
//            }
//        }


//        /// <summary>
//        /// 合并图片
//        /// </summary>
//        /// <param name="imgBack">背景图</param>
//        /// <param name="img">待合并图</param>
//        /// <param name="xDeviation">x轴偏移量</param>
//        /// <param name="yDeviation">y轴偏移量</param>
//        /// <returns></returns>
//        public static Bitmap CombinImage(System.Drawing.Image imgBack, System.Drawing.Image img, int xDeviation = 0, int yDeviation = 0)
//        {
//            Bitmap bitmap = new Bitmap(imgBack.Width, imgBack.Height);
//            Graphics graphics = Graphics.FromImage(bitmap);
//            graphics.Clear(Color.White);
//            graphics.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);
//            graphics.FillRectangle(Brushes.White, imgBack.Width / 2 - img.Width / 2 + xDeviation - 1, imgBack.Height / 2 - img.Height / 2 + yDeviation - 1, img.Width + 2, img.Height + 2);
//            graphics.DrawImage(img, imgBack.Width / 2 - img.Width / 2 + xDeviation, imgBack.Height / 2 - img.Height / 2 + yDeviation, img.Width, img.Height);
//            GC.Collect();
//            return bitmap;
//        }

//        /// <summary>
//        /// 合并图片
//        /// </summary>
//        /// <param name="imgBack">背景图</param>
//        /// <param name="img">待合并图</param>
//        /// <param name="saveFile">The save file.</param>
//        /// <param name="xDeviation">x轴偏移量</param>
//        /// <param name="yDeviation">y轴偏移量</param>
//        /// <returns></returns>
//        public static void CombinImage(string imgBack, string img, string saveFile, int xDeviation = 0, int yDeviation = 0)
//        {
//            System.Drawing.Image image = System.Drawing.Image.FromFile(imgBack);
//            System.Drawing.Image image2 = System.Drawing.Image.FromFile(img);
//            Bitmap bitmap = new Bitmap(image.Width, image.Height);
//            Graphics graphics = Graphics.FromImage(bitmap);
//            graphics.Clear(Color.White);
//            graphics.DrawImage(image, 0, 0, image.Width, image.Height);
//            graphics.FillRectangle(Brushes.White, image.Width / 2 - image2.Width / 2 + xDeviation - 1, image.Height / 2 - image2.Height / 2 + yDeviation - 1, image2.Width + 2, image2.Height + 2);
//            graphics.DrawImage(image2, image.Width / 2 - image2.Width / 2 + xDeviation, image.Height / 2 - image2.Height / 2 + yDeviation, image2.Width, image2.Height);
//            GC.Collect();
//            bitmap.Save(saveFile, ImageFormat.Png);
//            bitmap.Dispose();
//        }

//        /// <summary>  
//        /// Resize图片  
//        /// </summary>  
//        /// <param name="bmp">原始Bitmap</param>  
//        /// <param name="newW">新的宽度</param>  
//        /// <param name="newH">新的高度</param>  
//        /// <returns>处理以后的图片</returns>  
//        public static System.Drawing.Image ResizeImage(System.Drawing.Image bmp, int newW, int newH)
//        {
//            try
//            {
//                System.Drawing.Image image = new Bitmap(newW, newH);
//                Graphics graphics = Graphics.FromImage(image);
//                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
//                graphics.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
//                graphics.Dispose();
//                return image;
//            }
//            catch
//            {
//                return null;
//            }
//        }

//        /// <summary>
//        /// 将文本转换为图片  返回 bitmap 使用后 需自行释放
//        /// </summary>
//        /// <param name="msg">待转换的文本内容</param>
//        /// <param name="wid">图片宽度</param>
//        /// <param name="high">图片高度</param>
//        /// <param name="fontname">文本字体</param>
//        /// <param name="fontsize">文本字号</param>
//        /// <param name="fontstyle">文本样式</param>
//        /// <param name="fontColor">字体颜色</param>
//        /// <param name="bgColor">图片背景颜色</param>
//        /// <param name="pdleft">画布x偏移值</param>
//        /// <param name="pdtop">画布y偏移值</param>
//        /// <returns></returns>
//        public static Bitmap CreateImageByTxt(string msg, int wid, int high, string fontname = "微软雅黑", int fontsize = 14, FontStyle fontstyle = FontStyle.Regular, Color? fontColor = default(Color?), string bgColor = "#ffffff", int pdleft = 5, int pdtop = 5)
//        {
//            Color color = Color.Black;
//            if (fontColor.HasValue)
//            {
//                color = fontColor.Value;
//            }
//            Font font = new Font(fontname, (float)fontsize, fontstyle);
//            SolidBrush solidBrush = new SolidBrush(color);
//            Bitmap bitmap = new Bitmap(wid, high);
//            Graphics graphics = Graphics.FromImage(bitmap);
//            graphics.Clear(ColorTranslator.FromHtml(bgColor));
//            RectangleF layoutRectangle = new RectangleF((float)pdleft, (float)pdtop, (float)wid, (float)high);
//            graphics.DrawString(msg, font, solidBrush, layoutRectangle);
//            graphics.Dispose();
//            solidBrush.Dispose();
//            font.Dispose();
//            return bitmap;
//        }

//        /// <summary>
//        /// 将文本转换为图片  返回 bitmap 使用后 需自行释放, 返回ok 标识保存成功,其他为错误信息
//        /// </summary>
//        /// <param name="msg">待转换的文本内容</param>
//        /// <param name="wid">图片宽度</param>
//        /// <param name="high">图片高度</param>
//        /// <param name="saveFileFullName">文件保存全路径</param>
//        /// <param name="fontname">文本字体</param>
//        /// <param name="fontsize">文本字号</param>
//        /// <param name="fontstyle">文本样式</param>
//        /// <param name="fontColor">字体颜色</param>
//        /// <param name="bgColor">图片背景颜色</param>
//        /// <param name="pdleft">画布x偏移值</param>
//        /// <param name="pdtop">画布y偏移值</param>
//        /// <returns></returns>
//        public static string CreateImageByTxt(string msg, int wid, int high, string saveFileFullName, string fontname = "微软雅黑", int fontsize = 14, FontStyle fontstyle = FontStyle.Regular, Color? fontColor = default(Color?), string bgColor = "#fffff", int pdleft = 5, int pdtop = 5)
//        {
//            try
//            {
//                Color color = Color.Black;
//                if (fontColor.HasValue)
//                {
//                    color = fontColor.Value;
//                }
//                Font font = new Font(fontname, (float)fontsize, fontstyle);
//                SolidBrush solidBrush = new SolidBrush(color);
//                Bitmap bitmap = new Bitmap(wid, high);
//                Graphics graphics = Graphics.FromImage(bitmap);
//                graphics.Clear(ColorTranslator.FromHtml(bgColor));
//                RectangleF layoutRectangle = new RectangleF((float)pdleft, (float)pdtop, (float)wid, (float)high);
//                graphics.DrawString(msg, font, solidBrush, layoutRectangle);
//                graphics.Dispose();
//                solidBrush.Dispose();
//                font.Dispose();
//                bitmap.Save(saveFileFullName, ImageFormat.Jpeg);
//                bitmap.Dispose();
//                return "ok";
//            }
//            catch (Exception ex)
//            {
//                return ex.ToString();
//            }
//        }





//        /// <summary>
//        /// 图片处理为圆角
//        /// </summary>
//        /// <param name="image"></param>
//        /// <returns></returns>
//        public static System.Drawing.Image DrawTransparentRoundCornerImage(System.Drawing.Image image)
//        {
//            Bitmap bm = new Bitmap(image.Width, image.Height);
//            Graphics g = Graphics.FromImage(bm);
//            g.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, image.Width, image.Height));

//            using (System.Drawing.Drawing2D.GraphicsPath path = CreateRoundedRectanglePath(new Rectangle(0, 0, image.Width, image.Height), image.Width))
//            {
//                g.SetClip(path);
//            }

//            g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
//            g.Dispose();

//            return bm;
//        }
//        //设置图片四个边角弧度
//        private static System.Drawing.Drawing2D.GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
//        {
//            System.Drawing.Drawing2D.GraphicsPath roundedRect = new System.Drawing.Drawing2D.GraphicsPath();
//            roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
//            roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
//            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
//            roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
//            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
//            roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
//            roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
//            roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
//            roundedRect.CloseFigure();
//            return roundedRect;
//        }

//        /// <summary>
//        /// 图片缩放
//        /// </summary>
//        /// <param name="b"></param>
//        /// <param name="dstWidth"></param>
//        /// <param name="dstHeight"></param>
//        /// <returns></returns>
//        public static Bitmap ResizeBitmapV2(Bitmap b, int dstWidth, int dstHeight)
//        {
//            Bitmap dstImage = new Bitmap(dstWidth, dstHeight);
//            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dstImage);
//            //   设置插值模式 
//            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
//            //   设置平滑模式 
//            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//            g.SmoothingMode = SmoothingMode.HighQuality;
//            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//            //用Graphic的DrawImage方法通过设置大小绘制新的图片实现缩放
//            g.DrawImage(b, new Rectangle(0, 0, dstImage.Width, dstImage.Height), new Rectangle(0, 0, b.Width, b.Height), GraphicsUnit.Pixel);
//            g.Save();
//            g.Dispose();
//            return dstImage;
//        }

//        /// <summary>
//        /// 圆角生成（但是只能是一个角）
//        /// </summary>
//        /// <param name="image">源图片 Image</param>
//        /// <param name="roundCorner">圆角位置</param>
//        /// <returns>处理好的Image</returns>
//        public static Image CreateRoundedCorner(Image image, RoundRectanglePosition roundCorner)
//        {
//            Graphics g = Graphics.FromImage(image);
//            //保证图片质量
//            g.SmoothingMode = SmoothingMode.HighQuality;
//            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//            g.CompositingQuality = CompositingQuality.HighQuality;
//            Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
//            //构建圆角外部路径
//            GraphicsPath rectPath = CreateRoundRectanglePath(rect, image.Width / 2, roundCorner);

//            //圆角背景用白色填充
//            Brush b = new SolidBrush(Color.Transparent);
//            g.DrawPath(new Pen(b), rectPath);
//            g.FillPath(b, rectPath);
//            g.Dispose();
//            return image;
//        }

//        /// <summary>
//        /// 构建GraphicsPath路径
//        /// </summary>
//        /// <param name="rect"></param>
//        /// <param name="radius"></param>
//        /// <param name="rrPosition">图片圆角位置</param>
//        /// <returns>返回GraphicPath</returns>
//        private static GraphicsPath CreateRoundRectanglePath(Rectangle rect, int radius, RoundRectanglePosition rrPosition)
//        {
//            GraphicsPath rectPath = new GraphicsPath();
//            switch (rrPosition)
//            {
//                case RoundRectanglePosition.TopLeft:
//                    {
//                        rectPath.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90);
//                        rectPath.AddLine(rect.Left, rect.Top, rect.Left, rect.Top + radius);
//                        break;
//                    }
//                case RoundRectanglePosition.TopRight:
//                    {
//                        rectPath.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90);
//                        rectPath.AddLine(rect.Right, rect.Top, rect.Right - radius, rect.Top);
//                        break;
//                    }
//                case RoundRectanglePosition.BottomLeft:
//                    {
//                        rectPath.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
//                        rectPath.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Bottom);
//                        break;
//                    }
//                case RoundRectanglePosition.BottomRight:
//                    {
//                        rectPath.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
//                        rectPath.AddLine(rect.Right - radius, rect.Bottom, rect.Right, rect.Bottom);
//                        break;
//                    }
//                case RoundRectanglePosition.All:
//                    {
//                        rectPath.AddArc(rect.X, rect.Y, radius * 2, radius * 2, 180, 90);
//                        rectPath.AddLine(rect.X + radius, rect.Y, rect.Right - radius * 2, rect.Y);
//                        rectPath.AddArc(rect.X + rect.Width - radius * 2, rect.Y, radius * 2, radius * 2, 270, 90);
//                        rectPath.AddLine(rect.Right, rect.Y + radius * 2, rect.Right, rect.Y + rect.Height - radius * 2);
//                        rectPath.AddArc(rect.X + rect.Width - radius * 2, rect.Y + rect.Height - radius * 2, radius * 2, radius * 2, 0, 90);
//                        rectPath.AddLine(rect.Right - radius * 2, rect.Bottom, rect.X + radius * 2, rect.Bottom);
//                        rectPath.AddArc(rect.X, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
//                        rectPath.AddLine(rect.X, rect.Bottom - radius * 2, rect.X, rect.Y + radius * 2);
//                        rectPath.CloseFigure();
//                        break;
//                    }
//            }
//            return rectPath;
//        }
//    }

//    /// <summary>
//    /// 目标图片的圆角位置
//    /// </summary>
//    public enum RoundRectanglePosition
//    {
//        /// <summary>
//        /// 左上角
//        /// </summary>
//        TopLeft,
//        /// <summary>
//        /// 右上角
//        /// </summary>
//        TopRight,
//        /// <summary>
//        /// 左下角
//        /// </summary>
//        BottomLeft,
//        /// <summary>
//        /// 右下角
//        /// </summary>
//        BottomRight,

//        /// <summary>
//        /// 4个角
//        /// </summary>
//        All
//    }
//}
